package com.sri.test;

import com.sri.db.dao.BeanManager;
import com.sri.db.dao.search.SearchDAO;
import com.sri.db.dao.search.SearchDAOService;
import com.sri.db.dao.search.SearchDAOServiceFactory;
import com.sri.model.home.Home;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class DBTesting {
  Statement stmt = null;

  ResultSet rs = null;

  Connection conn = null;

  public void getConnection() {
    try {
      System.out.println("get Connection");
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection("jdbc:mysql://localhost/sri?user=root&password=");

      // Do something with the Connection

    } catch (SQLException ex) {
      // handle any errors
      System.out.println("SQLException: " + ex.getMessage());
      System.out.println("SQLState: " + ex.getSQLState());
      System.out.println("VendorError: " + ex.getErrorCode());
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  public void getData() throws SQLException {

    try {
      System.out.println("get Data");
      stmt = conn.createStatement();
      rs = stmt.executeQuery("SELECT property_name FROM t_property");

      // or alternatively, if you don't know ahead of time that
      // the query will be a SELECT...

      if (stmt.execute("SELECT property_name FROM t_property")) {
        rs = stmt.getResultSet();
      }

      while (rs.next()) {

        System.out.println("property name " + rs.getString(1));
      }

      // Now do something with the ResultSet ....
    } catch (SQLException ex) {
      // handle any errors
      System.out.println("SQLException: " + ex.getMessage());
      System.out.println("SQLState: " + ex.getSQLState());
      System.out.println("VendorError: " + ex.getErrorCode());
    } finally {
      // it is a good idea to release
      // resources in a finally{} block
      // in reverse-order of their creation
      // if they are no-longer needed

      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException sqlEx) { // ignore }

          rs = null;
        }

        if (stmt != null) {
          try {
            stmt.close();
          } catch (SQLException sqlEx) { // ignore }

            stmt = null;
          }
        }
      }
    }
  }

  public static void main(String[] args) throws Exception {
    DBTesting testing = new DBTesting();
    testing.getConnection();
    testing.getData();
    new BeanManager().setContext(new FileSystemXmlApplicationContext("E://rental//rental//webapp//rental//xmls//rentalAppContext.xml"));
    testDbByIBatis();
  }

  public static void testDbByIBatis() {
    System.out.println("configured started");
    new BeanManager().setContext(new FileSystemXmlApplicationContext("E://rental//rental//webapp//rental//xmls//rentalAppContext.xml"));
    System.out.println("rental beans configured ");
    SearchDAOService searchDAO = SearchDAOServiceFactory.getSearchDAO();
    try {
      Home home = searchDAO.getHome(1);
      System.out.println("home Name" + home.getName());
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
